package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dao.MemberDao;
import com.health.dao.OrderDao;
import com.health.dao.ReportDao;
import com.health.dao.SetmealDao;
import com.health.dto.ReportDto;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Report;
import com.health.service.ReportService;
import com.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Classname ReportServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/17 16:13
 * @Author RainGrd
 */
@Service(interfaceClass = ReportService.class)
@Transactional
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private SetmealDao setmealDao;


    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
        //获得当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天的日期
        String firstDay4ThisMonth =
                DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);

        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);

        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);

        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐（取前4）
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }

    @Override
    public int updateReport(Report report) {
        int result = 0;
        //通过会员id查询是否拥有这条记录
//        Report selectReport = reportDao.selectByMemberId(report.getMemberId());
        //判断体检报告ID是否等于0
        if (report.getId() != 0) {
            report.setReportLoadDate(new Date());
            log.info("更新时的report:{}", report);
            result = reportDao.updateReport(report);

        } else if (report.getMemberId() != null) {
            report.setReportLoadDate(new Date());
            report.setReportStatus(1);
            log.info("增加时的report:{}", report);
            result = reportDao.insertReport(report);
        }
//        log.info("查询出来的selectReport:{}", selectReport);
        //如果之前上传过的话，就进行更新操作
        /*if (selectReport != null) {

        }
        //如果之前没有上传过的话，就进行添加操作
        if (selectReport == null) {

        }*/
        return result;
    }

    @Override
    public int deleteById(Integer id) {
        return reportDao.deleteById(id);
    }

    @Override
    public Result queryReportByPhoneNumber(String phoneNumber) {
        return Result.success(memberDao.findByTelephone(phoneNumber));
    }

    @Override
    public PageResult queryReportListAll(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Report> reports = reportDao.selectReportListAll(queryPageBean);
        log.info("reports:{}", reports);
      /*  org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();*/
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        String currentPrincipalName = authentication.getName();
//        log.info("正在登录的用户:{}",currentPrincipalName);
        List<Report> result = reports.getResult();
        List<ReportDto> reportDtos = new ArrayList<>();
        for (Report report : result) {
            ReportDto reportDto = new ReportDto();
            BeanUtils.copyProperties(report, reportDto);
            log.info("reportDto:{}", reportDto);
            //封装会员对象,套餐对象,预约对象
            reportDto.setOrder(orderDao.selectOrderByMemberIdAndOrderStatus(reportDto.getMemberId(), reportDto.getSetmealId()));
            reportDto.setMember(memberDao.findById(reportDto.getMemberId()));
            reportDto.setSetmeal(setmealDao.selectSetmealById(reportDto.getSetmealId()));
            log.info("reportDto:{}", reportDto);
            reportDtos.add(reportDto);
        }
        log.info("reportDtos:{}", reportDtos);
        return new PageResult(reports.getTotal(), reportDtos);
    }

    @Override
    public Report queryReportById(Integer id) {
        return reportDao.selectReportById(id);
    }

}
